
<template lang="pug">
  .page
    .search-form.mb-10.pl-10.pr-10
      Row(type="flex" justify="start")
        Col.pr-20.mb-15(:md="{ span: 12 }" :lg="{ span: 12 }" :xl="{ span: 8 }" :xxl="{span: 6}")
          span.label-txt.w80 客户名称：
          Input(v-model="name" placeholder="请输入" clearable)
        Col.pr-20.mb-15(:md="{ span: 12 }" :lg="{ span: 12 }" :xl="{ span: 8 }" :xxl="{span: 6}")
          span.label-txt.w80 客户所属：
          .choose-saler
            Tooltip(placement="bottom" max-width="200" :disabled="!tipIsShow" theme="light")
              Input.input(v-model="dolderNames" type="text" readonly clearable @on-clear="salersClear")
              div(slot="content")
                .font-14 {{dolderNames}}
            .button(@click="chooseSaler") 选择
          //- Input(v-model="salerName" placeholder="请输入" clearable)
        //- Col.pr-20.mb-15(:md="{ span: 12 }" :lg="{ span: 12 }" :xl="{ span: 8 }" :xxl="{span: 6}")
        //-   span.label-txt.w80 企业性质：
        //-   Select(v-model="character"  placeholder="请选择" clearable)
        //-     Option(v-for="(item, index) in characterArr" :value="item.key" :key="index") {{ item.value }}
        //- Col.pr-20.mb-15(:md="{ span: 12 }" :lg="{ span: 12 }" :xl="{ span: 8 }" :xxl="{span: 6}")
        //-   span.label-txt.w80 企业规模：
        //-   Select(v-model="size"  placeholder="请选择" clearable)
        //-     Option(v-for="(item, index) in sizeArr" :value="item.key" :key="index") {{ item.value }}
        Col.pr-20.mb-15(:md="{ span: 12 }" :lg="{ span: 12 }" :xl="{ span: 8 }" :xxl="{span: 6}")
          span.label-txt.w80 所属行业：
          Select(v-model="businessType"  placeholder="请选择" clearable)
            Option(v-for="(item, index) in customerBusinesstype_arr" :value="item.key" :key="index") {{ item.value }}
        //- Col.pr-20.mb-15(:md="{ span: 12 }" :lg="{ span: 12 }" :xl="{ span: 8 }" :xxl="{span: 6}")
        //-   span.label-txt.w80 注册资本：
        //-   Input(type="number" v-model="registerCapital" clearable)
        Col.pr-20.mb-15(:md="{ span: 12 }" :lg="{ span: 12 }" :xl="{ span: 8 }" :xxl="{span: 6}")
          span.label-txt.w80 状态：
          RadioGroup.beautiful-radio-group(type="button" v-model="status")
            Radio(label="1") 正常
            Radio(label="2") 关闭
        Col.pr-20.mb-15(:md="{ span: 12 }" :lg="{ span: 12 }" :xl="{ span: 8 }" :xxl="{span: 6}")
          span.label-txt.w80 新建时间：
          DatePicker(:value="xinjianshijian" type="daterange" placement="bottom" placeholder="请选择日期范围" separator="至" @on-change="timeChange")
          Button.ml-15(type="info" @click="searchBtn") 查询
    .pl-20.pr-20.bg-white
      Table.hasTitle-table.beautiful-table(width="100%" :loading="loading" stripe :columns="setThead" :data="listData" @on-selection-change="tableSelectChange")
        div(slot="header")
          .pull-left.table-title-lefttxt
            span.leftText
              | 共识方案数:
              span.text-info.pl-5.pr-5.font-weight {{codeTotal}}
            Divider(type="vertical")
            span.leftText
              | 共识方案转化率:
              span.text-info.pl-5.pr-5.font-weight {{transRate}}%
          .pull-right.table-title-btns
            Button(v-if="hasExportPlan" type="info" @click="exportBtn") 导出
        template(slot-scope="{ row, index }" slot="customerName")
          .u-d-info(@click="gotoDetail(row.customerId)") {{row.customerName}}
        template(slot-scope="{ row, index }" slot="planFile")
          Button(size="small" @click="downPlanFile(row.requirementId)") 下载
        template(slot-scope="{ row, index }" slot="communicate")
          Button.mr-5(type="info" size="small" @click="addCommunication(row.customerId)") 添加
          Button(type="info" size="small" @click="checkCommunication(row.customerId)") 查看
        template(slot-scope="{ row, index }" slot="action")
          Button.mr-5(v-if="HRDC_PostBargain" type="text" size="small" @click="openCreateModal(row.customerId,row.requirementId,row.brief)") 添加合作需求
          //- Button(v-if="!row.isClosed && HRDC_CloseRequirement" type="text" size="small" @click="closeRequirement(row.requirementId)") 关闭
          Button(v-if="!row.isClosed && HRDC_CloseRequirement" type="text" size="small" @click="handleShowCloseModal(row)") 关闭
        .clearfix.pl-20.pr-20(slot="footer")
          Page(:total="codeTotal" :current="PageIndex" :page-size="PageSize" prev-text="上一页" next-text="下一页" @on-change="pageChangeFn" show-total @on-page-size-change="onpageSizeChange" show-sizer :page-size-opts="[10, 20, 30, 50, 100]")
    Modal(v-model="createModal" title="合作")
      Form(ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100")
        FormItem.mb-5(label="所属需求：")
          span {{requirementName}}
        FormItem.mb-5(label="合作名称：" prop="name")
          Input(v-model="formValidate.name" placeholder="请输入")
        FormItem.mb-5(label="合作价格：" prop="price")
          Input(v-model="formValidate.price" placeholder="请输入")
        FormItem.mb-5(label="合作内容：" prop="content")
          Input(v-model="formValidate.content" placeholder="请输入")
        FormItem.mb-5(label="谈判挑战：" prop="challenge")
          Input(v-model="formValidate.challenge" placeholder="请输入")
        FormItem.mb-5(label="谈判策略：" prop="strategy")
          Input(v-model="formValidate.strategy" placeholder="请输入")
        FormItem.mb-5(label="备注信息：")
          Input(v-model="formValidate.remark" type="textarea" placeholder="请输入")
        FormItem(label="完成时间：" prop="completeTime")
          DatePicker(:value="formValidate.completeTime" type="datetime" format="yyyy-MM-dd HH:mm" placeholder="请选择" style="display:block" @on-change="completeTimeChange")
      template(slot="footer")
        Button(type="info" @click="handleSubmit('formValidate')") 提交
        Button(@click="reviseModal = false") 取消
    createCommunication(ref="createCommunication" :customerId="customerId" :contact_arr="contact_arr" :communicationtype="communicationtype")
    checkCommunication(ref="checkCommunication" :customerId="customerId" :communicationList="communicationList" :communicationTotal="communicationTotal" @get-list="getCommunicationList")
    visibvleTreeList(ref="treeModal" :treeData="treeData" @on-change="getSalerId")
    CloseModal(ref="CloseModal" @query="getList")
</template>

<script>
import HRDCCustomerApi from '@/api/HRDCCustomer.api.js'
import Mixin from '../saleMixin.js'
import createCommunication from '@/view/oti-center/HRDC-common/communication/create.vue'
import checkCommunication from '@/view/oti-center/HRDC-common/communication/check.vue'
import CloseModal from '../close.vue'

export default {
  name: 'hrdc-demand-B',
  data () {
    return {
      transRate: 0, // 转化率
      name: '',
      salerName: '',
      character: '', // 企业性质
      size: '', // 企业规模
      businessType: '', // 所属行业
      registerCapital: '', // 注册资本
      requirementId: '', // 需求id
      requirementName: '', // 需求名称
      thead: ['selection', 'requirementCode', 'brief', 'customerName', 'planName', 'planFile', 'policymaker', 'policyEffector', 'purchaseFlow', 'createrName', 'status', 'businessTypeName', 'closedReason', 'closedByName', 'createTime', 'communicate', 'action'],
      listData: [],
      loading: false,
      codeTotal: 0,
      PageIndex: 1,
      PageSize: 10,
      status: '1',
      createModal: false,
      xinjianshijian: [], // 新建时间
      formValidate: {
        // customerId: '', // 客户id
        // requirementId : '', // 需求id
        name: '', // 合作名称
        price: '', // 合作价格
        content: '', // 合作内容
        challenge: '', // 谈判挑战
        strategy: '', // 谈判策略
        remark: '', // 备注信息
        completeTime: this.$moment(new Date()).format('YYYY-MM-DD HH:mm')
      },
      ruleValidate: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '价格不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ],
        challenge: [
          { required: true, message: '挑战不能为空', trigger: 'blur' }
        ],
        strategy: [
          { required: true, message: '策略不能为空', trigger: 'blur' }
        ],
        completeTime: [
          { required: true, message: '完成时间不能为空', trigger: 'change' }
        ]
      }
    }
  },
  mixins: [Mixin],
  components: {
    createCommunication,
    checkCommunication,
    CloseModal
  },
  computed: {
    setThead () {
      return this.getTableColumns(this.thead)
    },
    actionArr () {
      let arr = this.$store.state.user.actionAccess.map(item => {
        return item.code
      })
      return arr
    },
    // 添加合作需求
    HRDC_PostBargain () {
      return this.actionArr.indexOf('HRDC_PostBargain') > -1
    },
    // 关闭
    HRDC_CloseRequirement () {
      return this.actionArr.indexOf('HRDC_CloseRequirement') > -1
    },
    // 导出
    hasExportPlan () {
      return this.actionArr.indexOf('ExportPlan') > -1
    },
  },
  created () {
    this.getList()
  },
  watch: {
    $route (to, from) {
      if (to.name === 'hrdc-demand-B') {
        this.getList()
      }
    }
  },
  methods: {
    // 获取列表数据
    getList () {
      this.loading = true
      HRDCCustomerApi.GetPlanItems({
        name: this.name,
        salerName: this.salerName,
        HolderIds: this.salesIds,
        Character: this.character,
        Size: this.size,
        Status: this.status,
        BusinessType: this.businessType,
        RegisterCapital: this.registerCapital,
        RequirementBeginTime: this.xinjianshijian[0],
        RequirementEndTime: this.xinjianshijian[1],
        PageIndex: this.PageIndex,
        PageSize: this.PageSize
      }).then(res => {
        this.loading = false
        if (res.data.success) {
          this.listData = res.data.data
          this.codeTotal = res.data.totalRows
          this.transRate = res.data.extraInfo.transRate.toFixed(2)
        } else {
          
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },
    // 查询
    searchBtn () {
      this.PageIndex = 1
      this.getList()
    },
    openCreateModal (customerId, requirementId, name) {
      this.requirementName = name
      this.requirementId = requirementId
      this.customerId = customerId
      this.createModal = true
    },
    // 下载方案附件
    downPlanFile (id) {
      HRDCCustomerApi.DownPlanFile({
        requirementId: id
      }).then(res => {
        if (res.data.success) {
          let objectUrl = '/' + res.data.data.replace(/\\/g, '\/')
          console.log(objectUrl)
          const link = document.createElement('a')
          link.download = ''
          link.href = objectUrl
          link.click()
        } else {
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },
    // 提交按钮
    handleSubmit (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          this.postBargain()
          this.$refs[name].resetFields()
        } else {
          this.$Message.warning('有必填项未填!')
        }
      })
    },
    // 添加议价
    postBargain () {
      HRDCCustomerApi.PostBargain(Object.assign({
        customerId: this.customerId,
        requirementId: this.requirementId
      }, this.formValidate)).then(res => {
        if (res.data.success) {
          this.$Message.success(res.data.message)
          this.createModal = false
          this.formValidate = {
            name: '', // 合作名称
            price: '', // 合作价格
            content: '', // 合作内容
            challenge: '', // 谈判挑战
            strategy: '', // 谈判策略
            remark: '', // 备注信息
            completeTime: this.$moment(new Date()).format('YYYY-MM-DD HH:mm')
          }
        } else {
          
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },
    tableSelectChange () {

    },
    completeTimeChange (val) {
      this.formValidate.completeTime = val
    },
    pageChangeFn (val) {
      this.PageIndex = val
      this.getList()
    },
    onpageSizeChange (val) {
      this.PageSize = val
      this.getList()
    },

    handleShowCloseModal(row) {
      this.$refs.CloseModal.show(row)
      this.$refs.CloseModal.modalType = 'B'
    },

    // 客户导出
    exportBtn () {
      HRDCCustomerApi.exportPlan({
        selectedIds: this.ids,
        name: this.name,
        salerName: this.salerName,
        HolderIds: this.salesIds,
        Character: this.character,
        Size: this.size,
        Status: this.status,
        BusinessType: this.businessType,
        RegisterCapital: this.registerCapital,
        RequirementBeginTime: this.xinjianshijian[0],
        RequirementEndTime: this.xinjianshijian[1],
      }).then(res => {
        if (res.data.success) {
          let objectUrl = res.data.data
          console.log(objectUrl)
          const link = document.createElement('a')
          link.download = ''
          link.href = objectUrl
          link.click()
        } else {
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },

    timeChange (event) {
      console.log('时间改变 event: ', event)
      this.xinjianshijian = event
    }
  }
}
</script>

<style>

</style>
